package com.gzx.plugin.tjzy.modular.district.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gzx.plugin.tjzy.modular.district.entity.GzxRegionDistrict;
import com.gzx.plugin.tjzy.modular.district.mapper.GzxRegionDistrictMapper;
import com.gzx.plugin.tjzy.modular.district.service.GzxRegionDistrictListService;
import com.gzx.plugin.tjzy.modular.district.service.GzxRegionDistrictService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.cache.CommonCacheOperator;

/**
 * @author zjt
 * @description
 * @date 2024/12/18 17:11
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GzxRegionDistrictServiceImpl implements GzxRegionDistrictService {

    private final GzxRegionDistrictMapper regionDistrictMapper;

    private final GzxRegionDistrictListService regionDistrictListService;

    private final CommonCacheOperator cacheOperator;

    /**
     * 根据身份证号获取所属省市区
     *
     * @param cardId
     * @return
     */
    @Override
    @DS("tjzy")
    public GzxRegionDistrict getRegionDistrict(String cardId) {
        String regionDistrict = cacheOperator.getCatchString("RegionDistrict:" + cardId);
        if (StringUtils.isNotEmpty(regionDistrict)) {
            return JSON.parseObject(regionDistrict, GzxRegionDistrict.class);
        } else {
            GzxRegionDistrict obj = regionDistrictMapper.selectOne(new LambdaQueryWrapper<GzxRegionDistrict>().eq(GzxRegionDistrict::getCode, cardId));
            if (obj == null) {
                obj = new GzxRegionDistrict();
            }
            cacheOperator.setCatchString("RegionDistrict:" + obj.getCode(), JSON.toJSONString(obj));
            return obj;
        }
    }
}
